package com.skycong.hello77.activity.optionmenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;
import com.skycong.hello77.R;

import static com.skycong.hello77.R.menu.main;

/**
 * @author ruanmingcong
 * @date 2018/1/13 15:20
 */
public class OptionMenuActivity  extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_option_menu);
    }

    /**
     * 创建选项菜单
     * */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //创建菜单选项
        //静态添加菜单
        //getMenuInflater().inflate(R.menu.main,menu);

        //动态添加菜单
        // menu.add(1,100,1,"菜单一");
        // menu.add(1,101,1,"菜单二");
        // menu.add(1,102,1,"菜单三");
        // menu.add(1,103,1,"菜单四");
        // menu.add(1,104,1,"菜单五");
        /*
        * SubMenu子菜单
        * */
        SubMenu edit = menu.addSubMenu("编辑");
        SubMenu file = menu.addSubMenu("文件");
        edit.add(1,1,1,"新建");
        edit.add(1,2,1,"打开");
        edit.add(1,3,1,"保存");
        edit.setHeaderTitle("编辑操作");
        edit.setHeaderIcon(R.mipmap.icon_touxiang);
        file.add(2,1,1,"文件一");
        file.add(2,2,1,"文件二");
        file.add(2,3,1,"文件三");
        file.setHeaderTitle("文件操作");
        file.setHeaderIcon(R.mipmap.icon_touxiang);



        return true;
    }
   /**
    * 菜单的点击事项
    * */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu1:{
                Toast.makeText(OptionMenuActivity.this,"点击了菜单一",Toast.LENGTH_SHORT).show();
            }
            case R.id.menu2:{
                Toast.makeText(OptionMenuActivity.this,"点击了菜单二",Toast.LENGTH_SHORT).show();
            }

        }
        return super.onOptionsItemSelected(item);
    }
}
